

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Collections.Generic;
using JTC.MiniCommerce.Localization;

namespace JTC.MiniCommerce.Web {
  public class OrderProgressTemplate : TemplateControl, ITemplate {
    
    #region Member Variables
    
    private string _template;
    private List<Control> _controlCollection;
    
    #endregion
    
    #region Constructors

    /// <summary>
    /// Initializes a new instance of the <see cref="T:OrderProgressTemplate"/> class.
    /// </summary>
    public OrderProgressTemplate() {
      _controlCollection = new List<Control>();
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="T:OrderProgressTemplate"/> class.
    /// </summary>
    /// <param name="template">The template.</param>
    public OrderProgressTemplate(string template) {
      _template = template;
    }
    
    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets the control collection.
    /// </summary>
    /// <value>The control collection.</value>
    public List<Control> ControlCollection {
      get {
        return _controlCollection;
      }
      set {
        _controlCollection = value;
      }
    }
    
    #endregion
  
    #region ITemplate Members

    /// <summary>
    /// When implemented by a class, defines the <see cref="T:System.Web.UI.Control"></see> object that child controls and templates belong to. These child controls are in turn defined within an inline template.
    /// </summary>
    /// <param name="container">The <see cref="T:System.Web.UI.Control"></see> object to contain the instances of controls from the inline template.</param>
    public void InstantiateIn(Control container) {
      if(_template != null) {
        LiteralControl control = new LiteralControl(_template);
        container.Controls.Add(control);
      }
      else {
        if(_controlCollection.Count == 0) {
          BuildDefaultTemplate();
        }
        foreach(Control control in _controlCollection) {
          container.Controls.Add(control);
        }
      }
    }

    #endregion
    
    #region Methods
    
    #region Private

    /// <summary>
    /// Builds the default template.
    /// </summary>
    private void BuildDefaultTemplate() {
      LiteralControl loadingBox = new LiteralControl("<div class=\"loadingbox\">");
      Image spinner = new Image();
      spinner.ID = "imgSpinner";
      spinner.SkinID = "spinner";
      LiteralControl spacer = new LiteralControl("&nbsp;&nbsp;");
      Label lblUpdatingOrder = new Label();
      lblUpdatingOrder.ID = "lblUpdatingOrder";
      LocalizationUtility.AddLabelText(lblUpdatingOrder);
      lblUpdatingOrder.CssClass = "label";
      LiteralControl endLoadingBox = new LiteralControl("</div>");

      _controlCollection.Add(loadingBox);
      _controlCollection.Add(spinner);
      _controlCollection.Add(spacer);
      _controlCollection.Add(lblUpdatingOrder);
      _controlCollection.Add(endLoadingBox);
    }
    
    #endregion
    
    #endregion
    
  }
}
